package appapi.Global;

import appapi.common.PassToken;
import appapi.common.ResponseCodeEnum;
import appapi.service.IStaffService;
import appapi.service.StaffroleService;
import appapi.service.impl.StaffService;
import appapi.utils.JwtUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.ExpiredJwtException;
import io.swagger.models.HttpMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.HandlerMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/20 15:13
 */
@Component
public class MyJwtInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        Map<String, Object> map = new HashMap<>();
        //返回值
        Boolean bRet = true;
        // 获取请求头中的令牌
        String token = request.getHeader("token");
        try {
            //验证令牌
            bRet = JwtUtil.checkToken(token);
            if (!bRet) {
                map.put("code", 300);
                map.put("message", "token无效或过期");
            }
        } catch (SignatureVerificationException e) {
            map.put("code", 300);
            map.put("message", "无效签名");
            bRet = false;
        } catch (ExpiredJwtException e) {
            map.put("code", 300);
            map.put("message", "token过期");
            bRet = false;
        } catch (AlgorithmMismatchException e) {
            map.put("code", 300);
            map.put("message", "token算法不一致");
            bRet = false;
        } catch (BusinessException e) {
            map.put("code", 300);
            map.put("message", e.getMessage());
            bRet = false;
        } catch (Exception e) {
            map.put("code", 300);
            map.put("message", "token验证失败");
            bRet = false;
        }
        if(bRet) {
            map.put("code", 200);
            map.put("message", "验证通过");
            return  true;
        }
        //将map转为json
        String json = new ObjectMapper().writeValueAsString(map);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
        //表示接受任意域名的请求,也可以指定域名
        response.setHeader("Access-Control-Allow-Origin",request.getHeader("origin"));
       //该字段可选，是个布尔值，表示是否可以携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT,PATCH, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "*");
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())){
            return  true;
        }
        return false;
    }
}